package com.farecrawler.app.email;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Component;

import com.farecrawler.app.model.FlightInfo;

@Component("formatter")
public class Formatter {
	
	@Resource(name="stringBuilder")
	private StringBuilder templateBuilder;
	
	public void formatHtmlEmail(List<FlightInfo> infoList) {
		
		for(FlightInfo info : infoList) {
			fillFlightInfo(info);
		}
	}
	
	public String getEmailBody() {
		return finaliseEmailBody();
	}
	
	/**
	 * HTML template for table headers and values
	 */
	protected void getHtmlEmailTemplate() {
		
		templateBuilder.append("<table cellspacing=30>");
		templateBuilder.append("<tr style='font-weight:bold;text-decoration:underline;'>");
		templateBuilder.append("<td>Flight Date</td>");
		templateBuilder.append("<td>Flight Number</td>");
		templateBuilder.append("<td>Departure Time</td>");
		templateBuilder.append("<td>Arrival Time</td>");
		templateBuilder.append("<td>Price</td>");
		templateBuilder.append("</tr>");
	}
	
	/**
	 * Populate HTML table with the necessary row items and values.
	 * 
	 * @param flightInfo
	 */
	protected void fillFlightInfo(FlightInfo flightInfo) {
		
		getHtmlEmailTemplate();
		templateBuilder.append("<tr>");
		templateBuilder.append("<td>");
		templateBuilder.append(flightInfo.getFlightDate());
		templateBuilder.append("</td>");
		templateBuilder.append("<td>");
		templateBuilder.append(flightInfo.getFlight());
		templateBuilder.append("</td>");
		templateBuilder.append("<td>");
		templateBuilder.append(flightInfo.getDepartureTime());
		templateBuilder.append("</td>");
		templateBuilder.append("<td>");
		templateBuilder.append(flightInfo.getArrivalTime());
		templateBuilder.append("</td>");
		templateBuilder.append("<td>");
		templateBuilder.append(flightInfo.getTicketPrice());
		templateBuilder.append("</td>");
		templateBuilder.append("</tr>");
	}
	
	/**
	 * Close HTML table with closing <//table> tag and return the entire HTML as a String
	 * 
	 * @return
	 */
	protected String finaliseEmailBody() {
		templateBuilder.append("</table>");
		return templateBuilder.toString();
	}

	/*
	 * Getters Setters
	 */
	public StringBuilder getTemplateBuilder() {
		return templateBuilder;
	}
	
	public void setTemplateBuilder(StringBuilder templateBuilder) {
		this.templateBuilder = templateBuilder;
	}

}
